<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{show}">
      <span class="placeholder">{{fullLocation}}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="show">
      <span class="ellipsis" @click="changeItem(item)" v-for="item in currList" :key="item.code">{{item.name}}</span>
    </div>
  </div>
</template>

<script>
import { computed, reactive, ref } from 'vue'
import {onClickOutside, } from '@vueuse/core'
import axios from 'axios'
export default {
  name: "XtxCity",
  props:{
    fullLocation:{
      type:Object
    }
  },
  setup(props,{emit}){
    //  需求1 显示隐藏
  const show = ref(false)
  const target = ref(null)
  const cityData = ref([])
  
  // 函数，除了显示隐藏还可以干其他事
  const open = (data) => {
    show.value = true
    getCityDate()
    getCityDate().then(data=>{
      cityData.value = data
    })
  }
  const close = () => {
    show.value = false
  }
  // 可以切换
  const toggle = () => {
    if(show.value) close()
    else open()
  }
  // 需求2  点击其他地方也可以隐藏 使用@vueuse
  onClickOutside(target,()=>{
    close()
  })
  // 需求3  遍历城市,也就要先获取城市数据
  // 得到的是一个promise，需要.then进行处理
  const getCityDate = () => {
    return new Promise((resolve,reject)=>{
      // 处理缓存，减少请求次数，减轻服务器压力
      if(window.cityData){
        resolve(window.cityData)
      }else{
        // 将数据放在缓存里面
        const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
        axios.get(url).then(res=>{
        console.log(res.data)
        window.cityData = res.data
        resolve(window.cityData)
      })
      }
    })
    
  }
  // 需求5 获取点击的城市数据存储起来，判断level是0,1,2
  const changeResult = reactive({
    provinceCode:'',
    provinceName:'',
    cityCode:'',
    cityName:'',
    countyCode:'',
    countyName:'',
    fullLocation:''
  })
  const changeItem = (item) => {
    if(item.level === 0){
      changeResult.provinceCode = item.code
      changeResult.provinceName = item.name
    }if(item.level === 1){
      changeResult.cityCode = item.code
      changeResult.cityName = item.name
    }if(item.level === 2){
      changeResult.countyCode = item.code
      changeResult.countyName = item.name
      changeResult.fullLocation = `${changeResult.provinceName} ${changeResult.cityName} ${changeResult.countyName}`
      close()
      // 传给父组件啊，又忘，无语
      emit('change',changeResult)
    }
  }
  // 计算属性--省市区
  const currList = computed(()=>{
    // 默认省份
    let currList = cityData.value
    if(changeResult.provinceCode && changeResult.provinceName){
      currList = currList.find(a => a.code === changeResult.provinceCode).areaList
    }if(changeResult.cityCode && changeResult.cityName){
      currList = currList.find(b => b.code === changeResult.cityCode).areaList
    }
    return currList
    
  })
  return {toggle, show, target, cityData, getCityDate, changeItem, currList}
  }
  
}


</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
